package com.xinzhi.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.xinzhi.mapper.AppraisalMapper;
import com.xinzhi.mapper.AppraisalssMapper;
import com.xinzhi.mapper.PointAppraisalMapper;
import com.xinzhi.pojo.Appraisal;
import com.xinzhi.pojo.dto.*;
import com.xinzhi.service.AppraisalService;
import com.xinzhi.utils.RedisUtils;
import com.xinzhi.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppraisalServiceImpl implements AppraisalService {
    @Autowired
    private AppraisalMapper appraisalMapper;
    @Autowired
    private PointAppraisalMapper pointAppraisalMapper;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 特殊扣分申请新增接口
     * @param object
     * @param demerit
     * @param score
     * @return
     */

    @Override
    public Result indexKou(String object, String demerit, Integer score) {
        if (object == null){
            return Result.error("考核对象不能为空");
        }
        if (demerit == null){
            return Result.error("考核扣分不能为空");
        }
        if (score >10){
            return Result.error("考核扣分不能超过10分");
        }
        int i = appraisalMapper.addKou(object, demerit, score);
        return i > 0 ? Result.ok("新增成功") : Result.error("新增失败");
    }

    /**
     * 特殊加分申请根据查询接口
     * @param object
     * @return
     */
    @Override
    public Result getAppraisalByObject(String object) {
        //先去查考核对象
        Appraisal appraisal = appraisalMapper.selectByObject(object);
        //再去判断数据库中有没有，没有就直接返回
        if (appraisal == null){
            return Result.error("没有你所查的考核对象");
        }

        String rediskay = "userlist"+object;
        //先去Redis中查询数据
        String userlist = (String) redisUtils.get(rediskay);
        //进行Redis判断
        if (StrUtil.isNotEmpty(userlist)){
            return Result.ok(userlist);
        }

        //对参数进行判断
        if (object ==null){
            return Result.error("查询标题不能为空");
        }
        //进行数据库查询
        List<Teshujiafens> appraisalByObject = appraisalMapper.getAppraisalByObject(object);
        if (appraisalByObject !=null){
            redisUtils.set(rediskay, JSONUtil.toJsonStr(appraisalByObject));
            return Result.ok(appraisalByObject);
        }else {
            return Result.error("数据库中没有这条数据");
        }
    }

    /**
     * 特殊加分申请查询接口
     * @return
     */
    @Override
    public Result getAppraisalAll() {
        List<Teshujiafens> appraisalAll = appraisalMapper.getAppraisalAll();

        return appraisalAll != null ? Result.ok(appraisalAll) : Result.error("数据库中没有这条数据");
    }

    /**
     * 特殊扣分申请查询接口
     * @return
     */
    @Override
    public Result getKoufenAll() {
        List<Teshujiafen> koufenAll = appraisalMapper.getKoufenAll();
        return koufenAll != null ? Result.ok(koufenAll) : Result.error("数据库中没有这条数据");
    }


    /**
     * 特殊申请扣分查询接口
     * @param object
     * @return
     */
    @Override
    public Result AppraisalService(String object) {
        //先去redis里查询这条数据存不存在
        String userKey = "user:" +object;
        String existingUserJson = (String) redisUtils.get(userKey);
        if (StrUtil.isNotBlank(existingUserJson)) {
            return Result.ok(existingUserJson);
        }
        if (object==null){
            return Result.error("没有数据");
        }
        //如果redis里没有，则从数据库中查询
        // List byAccount1 = appraisalMapper.findByAccount1(object);
        List<Dtokoufen> querystudent = appraisalMapper.querystuden(object);

        if (querystudent != null){
        //将查询到的数据存入redis中
            redisUtils.set(userKey, JSONUtil.toJsonStr(querystudent));
            return Result.ok(querystudent); }
        return Result.error("没有数据");
    }


    /**
     * 特殊加扣分申请逻辑删除接口
     * @param demerit
     * @param deleted
     * @return
     */
    @Override
    public Result updateAppraisal(String demerit ,Integer deleted) {
        if (deleted != null && deleted == 1) {
            int updateappraisal = appraisalMapper.updateappraisal(demerit, deleted);
            return updateappraisal > 0 ? Result.ok("删除成功") : Result.error("删除失败");
        } else {
            return Result.error("删除失败");
        }
    }


    /**
     * 特殊扣分申诉审核查询接口
     * @return
     */
    @Override
    public Result selectAlls() {
        String appeal = (String) redisUtils.get("申诉审核查询");
        if (StrUtil.isNotEmpty(appeal)){
            return Result.ok(appeal);
        }
        List<Dtoshensukou> appraisals = pointAppraisalMapper.querystuden2();
        if (appraisals != null){
            redisUtils.set("申诉审核查询", JSONUtil.toJsonStr(appraisals));
        }
        return Result.ok(appraisals);
    }


    /**
     * 特殊扣分申请根据年份查询接口哦
     * @param object
     * @param year
     * @return
     */

    @Override
    public Result selectAllsByYear(String object, String year) {
        Appraisal appraisal = appraisalMapper.selectByyear(year);
        if (appraisal == null){
            return Result.error("没有相关数据");
        }
        String appeals = (String) redisUtils.get("特殊扣分申请根据年份查询");
        if (StrUtil.isNotEmpty(appeals)){
            return Result.ok(appeals);
        }
        if (object == null && year == null){
            return Result.error("查询条件不能为空");
        }
        List<Dtokoufen> dtokoufens = appraisalMapper.querystuden2(object, year);
        if (dtokoufens == null){
            return Result.error("没有相关数据");
        }
        return dtokoufens != null ? Result.ok(dtokoufens) : Result.error("没有相关数据");
    }





}
